package com.hairline.service;

import com.hairline.bean.Category;
import com.hairline.bean.CategoryExample;
import com.hairline.bean.vo.WxCatalogCurrentVo;
import com.hairline.bean.vo.WxCatalogIndexVo;
import com.hairline.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fenglin on 2020/9/4 11:32 AM
 */
@Service
public class WxCatalogServiceImpl implements  WxCatalogService{
    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 获取目录商品信息
     * @return
     */
    @Override
    public WxCatalogIndexVo catalogIndex() {
        //获取全部目录类商品信息
        CategoryExample exampleParent = new CategoryExample();
        CategoryExample.Criteria criteria = exampleParent.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(exampleParent);
        //获取第一行数据id，并将第一行数据作为当前行
        int id = categoryList.get(0).getId();
        Category currentCategory = categoryMapper.selectByPrimaryKey(id);
        //获取当前目录类商品的所有子类商品
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria1 = example.createCriteria();
        //子类商品的pid = 目录类商品的id
        criteria1.andPidEqualTo(id);
        criteria1.andDeletedEqualTo(false);
        List<Category> currentSubCategory = categoryMapper.selectByExample(example);
        WxCatalogIndexVo wxCatalogIndexVo = new WxCatalogIndexVo(currentCategory, categoryList, currentSubCategory);
        return wxCatalogIndexVo;
    }

    /**
     * 显示当前目录类内容
     * @param id 当前目录id
     * @return
     */
    @Override
    public WxCatalogCurrentVo catalogCurrent(Integer id) {
        //获取当前目录类
        Category currentCategory = categoryMapper.selectByPrimaryKey(id);
        //获取当前目录类对应子类商品;子类商品的pid = 目录类商品的id
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria1 = example.createCriteria();
        criteria1.andPidEqualTo(id);
        List<Category> currentSubCategory = categoryMapper.selectByExample(example);
        WxCatalogCurrentVo wxCatalogCurrentVo = new WxCatalogCurrentVo(currentCategory,currentSubCategory);
        return wxCatalogCurrentVo;
    }
}
